package com.atguigu.gulimall.order.service.impl;

import com.atguigu.gulimall.order.entity.OrderEntity;
import com.atguigu.gulimall.order.entity.OrderReturnReasonEntity;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.order.dao.OrderItemDao;
import com.atguigu.gulimall.order.entity.OrderItemEntity;
import com.atguigu.gulimall.order.service.OrderItemService;


@Service("orderItemService")
public class OrderItemServiceImpl extends ServiceImpl<OrderItemDao, OrderItemEntity> implements OrderItemService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<OrderItemEntity> page = this.page(
                new Query<OrderItemEntity>().getPage(params),
                new QueryWrapper<OrderItemEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * queues:声明需要监听的所有队列
     * 参数可以写以下类型：
     * 1、Message message ：原生消息详细信息：头+体
     * 2、T<发送的消息类型> OrderReturnReasonEntity entity  spring自动转化为我们发送的消息类型对象
     * 3、Channel channel:当前传输数据的通道
     *
     * Queue:可以很多人都来监听。只要收到消息，队列就会删除该消息，只能有一个收到此消息。
     * 场景：
     * 1）订单服务启动多个:同一个消息，只能有一个客户端接到
     * 2）只有一个消息完全处理完，方法运行结束，我们才可以接受到下一个消息
     *
     */

    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage(Message message, OrderReturnReasonEntity entity, Channel channel) throws InterruptedException {
        System.out.println("接到消息"+"OrderReturnReasonEntity:" + entity);
//        Thread.sleep(3000);
        System.out.println("消息处理完成=》"+entity.getName());
        //deliveryTag是channel内按顺序自增的
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            if(deliveryTag % 2 == 0){
                //利用deliveryTag将接受到并处理完后的消息进行ack回复。
                //收货
                channel.basicAck(deliveryTag, false);
                System.out.println("签收["+deliveryTag+"]号信息");
            }else{
                //退货，拒签.requeue=false 丢弃；requeue=ture 发回服务器，服务器重新入队
                // void basicNack(long deliveryTag, boolean multiple, boolean requeue)
                channel.basicNack(deliveryTag,false,true);
                System.out.println("不签收["+deliveryTag+"]号信息");
            }

        }catch (Exception e){
            //比如网络中断，该ack发不出去
            System.out.println(e);
        }


    }

    @RabbitListener(queues = {"hello-java-queue"})
    @RabbitHandler
    public void recieveMessage2(OrderEntity entity) throws InterruptedException {
        System.out.println("接到消息"+"OrderEntity:" + entity);
        System.out.println("消息处理完成=》"+entity.getOrderSn());

    }

}